import React from 'react'
import { inject, observer } from 'mobx-react';
import { toJS } from "mobx"
import isEqual from 'lodash'
import { Form, RouteLayout } from 'weaver-mobile-page';
import { Tools } from 'weaver-mobile'
import { withRouter } from 'react-router-dom'

const ButtonGroup = RouteLayout.ButtonGroup;
const getLabel = Tools.getLabel;
@inject('wfAgentStore')
@withRouter
@observer
export default class AdvancedItem extends React.Component {
    componentWillMount() {
    }
    getFields = () => {
        const { advancedItem } = this.props.wfAgentStore;
        const { form } = advancedItem;
        const { conditioninfo, isInit } = form;
        const fields = [];
        if (isInit) {
            conditioninfo.forEach((group) => {
                group.items.forEach((field) => {
                    fields.push(<Form.Item
                        key={field.domkey.join('__')}
                        form={form}
                        fieldConfig={field}
                        onChange={form.onChange}
                    />);
                });
            });
        }
        return fields;
    }

    onChange = (condition, value) => {
        const { advancedItem, initDatas, setInitDatas } = this.props.wfAgentStore;
        const { defaultAdvancedValue } = initDatas;
        const { form, parentPath } = advancedItem;
        form.onChange(condition, value);

        let obj1 = toJS(defaultAdvancedValue) || {};
        let obj2 = toJS(form.formParams);
        let compare = true;//默认相同
        if (parentPath === "/agent/mineAgentSet" || parentPath === "/agent/all") {
            compare = obj1.agentstatus === obj2.agentstatus &&
                obj1.agentuid === obj2.agentuid &&
                obj1.agentuidspan === obj2.agentuidspan &&
                obj1.aiddepartmentid === obj2.aiddepartmentid &&
                obj1.aiddepartmentidspan === obj2.aiddepartmentidspan &&
                obj1.aidsubcompanyid === obj2.aidsubcompanyid &&
                obj1.aidsubcompanyidspan === obj2.aidsubcompanyidspan &&
                obj1.bagentuid === obj2.bagentuid &&
                obj1.bagentuidspan === obj2.bagentuidspan &&
                obj1.selectBeginDate === obj2.selectBeginDate &&
                obj1.selectEndDate === obj2.selectEndDate &&
                obj1.wfidbytype === obj2.wfidbytype &&
                obj1.wfidbytypespan === obj2.wfidbytypespan &&
                obj1.wftype === obj2.wftype &&
                obj1.wftypespan === obj2.wftypespan;
        } else if (parentPath === "/agent/hadAgented" || parentPath === "/agent/toOthersAgent") {
            compare = obj1.creatertype === obj2.creatertype &&
                obj1.requestlevel === obj2.requestlevel &&
                obj1.requestname === obj2.requestname &&
                obj1.workcode === obj2.workcode &&
                toJS(obj1.createrid) === toJS(obj2.createrid);
            const { setMobxStore } = this.props.wfAgentStore;
            setMobxStore({ searchValue: obj2.requestname });
            let ssStr = "";
            if (parentPath === "/agent/hadAgented") {
                ssStr = "hadAgented";
            } else if (parentPath === "/agent/toOthersAgent") {
                ssStr = "toOthersAgent";
            }
        } else if (parentPath === "importFromTem") {
            compare = obj1.name === obj2.name &&
                obj1.workflowid === obj2.workflowid &&
                toJS(obj1.agenterid) === toJS(obj2.agenterid);
            const { setMobxStore } = this.props.wfAgentStore;
            setMobxStore({ searchValue: obj2.name });
            let ssStr = "importFromTem";
        } else if (parentPath === "temManagement") {
            compare = obj1.name === obj2.name &&
                obj1.workflowid === obj2.workflowid &&
                toJS(obj1.agenterid) === toJS(obj2.agenterid);
            const { setMobxStore } = this.props.wfAgentStore;
            setMobxStore({ searchValue: obj2.name });
            let ssStr = "temManagement";
        }
        if (compare) {
            setInitDatas({ isAdVancedHasData: false })
        } else {
            setInitDatas({ isAdVancedHasData: true })
        }
    }

    render() {
        const { advancedItem, setAdvancedItem } = this.props.wfAgentStore;
        const { form, parentPath } = advancedItem;
        return (// 
            <div className="advancedItem" style={{height:"100%"}}>
                <div style={{ height: "calc(100% - 47px)", overflowY:"auto" }}>
                    <Form store={form} onChange={this.onChange} />
                </div>
                <div className='wf-agent-advancedItem-button-group'>
                    <ButtonGroup
                        data={[
                            {
                                key: 'reset',
                                text: getLabel(2022,"重置"),
                                className: "wf-agent-advancedItem-button-reset",
                                onClick: () => {
                                    const { setInitDatas, setMobxStore, initDatas, getAgentDatas, tempManagement, setTempMDatas, importFromTem, agentTemplateList, setImportFromTemData, agentTemplateCondition } = this.props.wfAgentStore;
                                    const { listParams } = initDatas;
                                    const { form, formValue, conditioninfo, } = this.props.wfAgentStore.advancedItem;
                                    let formData = toJS(form.formData)
                                    form.resetFields()
                                    if (parentPath === "/agent/mineAgentSet") {
                                        form.updateFields(formValue);
                                    }
                                    setInitDatas({ isAdVancedHasData: false, searchParams: {} })
                                    setMobxStore({ searchValue: "" })
                                    setTempMDatas({ searchParams: {} })
                                    setImportFromTemData({ searchParams: {} })
                                },
                            },
                            {
                                key: 'save',
                                text: getLabel(826,"确定"),
                                onClick: () => {
                                    const { advancedItem, agentTemplate, setLocalStorage, setTempMDatas, setImportFromTemData, agentTemplateList, getAgentDatas, initDatas, setInitDatas } = this.props.wfAgentStore;
                                    const { listParams } = initDatas;
                                    const { parentPath } = advancedItem;
                                    if (parentPath === "temManagement") {
                                        agentTemplateList(form.formParams);//刷新列表
                                        if (form.formParams.name) {
                                            setLocalStorage("temManagement", form.formParams.name)//保存搜索历史
                                        }
                                        setTempMDatas({ searchParams: form.formParams })
                                        this.props.history.go(-1);//返回上一页
                                    } else if (parentPath === "importFromTem") {
                                        this.props.history.go(-1);
                                        agentTemplate(form.formParams)
                                        if (form.formParams.name) {
                                            setLocalStorage("importFromTem", form.formParams.name)
                                        }
                                        setImportFromTemData({ isAdvancedSearching: true, selectedRowKeys: "", searchParams: form.formParams })
                                    } else if (parentPath === "/agent/mineAgentSet" || parentPath === "/agent/hadAgented" || parentPath === "/agent/toOthersAgent" || parentPath === "/agent/all") {
                                        let params = {
                                            ...form.formParams,
                                            ...listParams,
                                        }
                                        if (parentPath === "/agent/hadAgented") {
                                            if (form.formParams.requestname) {
                                                setLocalStorage("hadAgented", form.formParams.requestname)
                                            }
                                        } else if (parentPath === "/agent/toOthersAgent") {
                                            if (form.formParams.requestname) {
                                                setLocalStorage("toOthersAgent", form.formParams.requestname)
                                            }
                                        }
                                        setInitDatas({ searchParams: form.formData })
                                        getAgentDatas(params);
                                        this.props.history.go(-1);
                                    }
                                },
                                type: "primary"
                            }
                        ]}
                    />
                </div>
            </div>
        )
    }

}